﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CustomLoggerTarget.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Helper class for WPF textbox target
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Lib.Helpers
{
    using System;
    using System.Globalization;
    using System.Text;
    using System.Windows.Controls;

    using NLog;
    using NLog.Config;

    /// <summary>
    /// Helper class for WPF textbox target
    /// </summary>
    public class CustomLoggerTarget : NLog.Targets.Target
    {
        #region Static Fields

        /// <summary>
        /// Singleton private instance
        /// </summary>
        private static CustomLoggerTarget instance;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Prevents a default instance of the <see cref="CustomLoggerTarget"/> class from being created.
        /// </summary>
        private CustomLoggerTarget()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets singleton public access
        /// </summary>
        public static CustomLoggerTarget Instance
        {
            get
            {
                return instance ?? (instance = new CustomLoggerTarget());
            }
        }

        /// <summary>
        /// Gets or sets the rule.
        /// </summary>
        public LoggingRule Rule { get; set; }

        /// <summary>
        /// Gets or sets the target text box.
        /// </summary>
        public TextBox TargetTextBox { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Method writes log event to text box.
        /// </summary>
        /// <param name="logEvent">
        /// The log event.
        /// </param>
        protected override void Write(LogEventInfo logEvent)
        {
            if (this.TargetTextBox == null)
            {
                return;
            }
            
            // StringBuilder works much faster, espacilly on another thread
            StringBuilder builder = new StringBuilder("[");
            builder.Append(
                (logEvent.TimeStamp.Hour < 10)
                    ? "0" + logEvent.TimeStamp.Hour
                    : logEvent.TimeStamp.Hour.ToString(CultureInfo.InvariantCulture));
            builder.Append(":");
            builder.Append(
                (logEvent.TimeStamp.Minute < 10)
                    ? "0" + logEvent.TimeStamp.Minute
                    : logEvent.TimeStamp.Minute.ToString(CultureInfo.InvariantCulture));
            builder.Append(":");
            builder.Append(
                (logEvent.TimeStamp.Second < 10)
                    ? "0" + logEvent.TimeStamp.Second
                    : logEvent.TimeStamp.Second.ToString(CultureInfo.InvariantCulture));
            builder.Append("][");
            builder.Append(logEvent.Level);
            builder.Append("] ");
            builder.Append(logEvent.FormattedMessage);
            builder.Append("\n");

            if (System.Windows.Application.Current.Dispatcher.CheckAccess() == false)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(
                    new Action(
                        () =>
                            {
                                this.TargetTextBox.AppendText(builder.ToString());
                                this.TargetTextBox.ScrollToEnd();
                            }));
            }
            else
            {
                this.TargetTextBox.AppendText(builder.ToString());
                this.TargetTextBox.ScrollToEnd();
            }
        }

        #endregion
    }
}